package com.tywj.buscustomerapp.broadcast;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.utils.MyActivityManager;

public class NetWorkStateReceiver extends BroadcastReceiver {
    private AlertDialog build=null;
    public static NetWorkStateReceiver netWorkStateReceiver = new NetWorkStateReceiver();

    public static NetWorkStateReceiver getInstance(){
        return netWorkStateReceiver;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        //检测API是不是小于23，因为到了API23之后getNetworkInfo(int networkType)方法被弃用
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager
                .getActiveNetworkInfo();
        if (activeNetworkInfo!=null && activeNetworkInfo.isConnected()){

        }else{
            showDialog("未连接网络，请检查Wi-Fi或数据是否打开");
        }
    }

    private void showDialog(final String title){
        final Activity activity = MyActivityManager.getInstance().getCurrentActivity();
        if (build==null && activity!=null && activity.hasWindowFocus()) {
            build = new AlertDialog.Builder(activity, R.style.AlertDialogStyle).create();
            //自定义布局
            View view = activity.getLayoutInflater().inflate(R.layout.dialog_layout, null);
            build.setView(view, 0, 0, 0, 0);
            build.show();
            //得到当前显示设备的宽度，单位是像素
            int width = activity.getWindowManager().getDefaultDisplay().getWidth();
            //得到这个dialog界面的参数对象
            WindowManager.LayoutParams params = build.getWindow().getAttributes();
            //设置dialog的界面宽度
            params.width = width - (width / 3);
            //设置dialog高度为包裹内容
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            //设置dialog的重心
            params.gravity = Gravity.CENTER;
            build.getWindow().setAttributes(params);
            TextView titleTv = view.findViewById(R.id.message);
            titleTv.setText(title);
            TextView messageTv = view.findViewById(R.id.message1);
            messageTv.setText("");
            Button btn_sure = view.findViewById(R.id.yes);
            Button btn_cancel = view.findViewById(R.id.no);
            btn_sure.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("android.settings.WIRELESS_SETTINGS");
                    activity.startActivity(intent);
                    build.dismiss();
                    build=null;
                    return;
                }
            });
            btn_cancel.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    build.dismiss();
                    build=null;
                }
            });
        }
    }


}
